package com.incar.services;

import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;

import androidx.annotation.Nullable;

import com.iflytek.aiui.AIUIAgent;
import com.iflytek.aiui.AIUIConstant;
import com.iflytek.aiui.AIUIEvent;
import com.iflytek.aiui.AIUIListener;
import com.iflytek.aiui.AIUIMessage;
import com.incar.util.FileUtil;
import com.incar.util.FucUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.Set;

public class AIUIservice extends Service {
    private static final String TAG = "AIUIservice";
    private AIUIAgent mAIUIAgent = null;
    private boolean mIsWakeupEnable = false;
    private int mAIUIState = AIUIConstant.STATE_IDLE;
    private String mSyncSid = "";
    private boolean mIsAIUI_V2;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initSDK();
        return super.onStartCommand(intent, flags, startId);
    }

    public void initSDK() {
        // 示例代码：初始化SDK
        //创建AIUIAgent
        mAIUIAgent = AIUIAgent.createAgent( this, getAIUIParams(), mAIUIListener );
//开始录音
        AIUIMessage msg = new AIUIMessage(AIUIConstant.CMD_START_RECORD, 0 ,0, "data_type=audio,sample_rate=16000", null);
        mAIUIAgent.sendMessage(msg);
    }
    private AIUIListener mAIUIListener = new AIUIListener() {
        @Override
        public void onEvent(AIUIEvent event) {
            switch (event.eventType) {
                case AIUIConstant.EVENT_CONNECTED_TO_SERVER:
                    Log.d(TAG, "onEvent:已连接服务器 ");
                    break;

                case AIUIConstant.EVENT_SERVER_DISCONNECTED:
                    Log.d(TAG, "onEvent: 与服务器断连");
                    break;
                //唤醒事件
                case AIUIConstant.EVENT_WAKEUP: {
                    String info = event.info;
                    Log.i(TAG, "on EVENT_WAKEUP: " + info);
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            //  Uri.parse("turing://com.turing.chat/assistant?type=0"));  //语音助手页
                            Uri.parse("turing://com.turing.chat/assistant?type=1"));  //唤醒页
                    //intent.setComponent(new ComponentName("com.turing.turingzhiwalauncher.assistant","com.turing.turingzhiwalauncher.assistant.ui.activity.AssistantChatActivity"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("wakeUpSource","forSystemVoice");

                    getApplicationContext().startActivity(intent);
                    if(info != null &&  !info.isEmpty()){
                        JSONObject jsInfo = null,ivwInfo =null;
                        String keyword="";
                        try {
                            jsInfo = new JSONObject(info);
                            String ivwResult = jsInfo.getString("ivw_result");
                            ivwInfo = new JSONObject(ivwResult);
                            keyword = ivwInfo.getString("keyword");

                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        Log.d(TAG, "onEvent: "+keyword);
                    }
                }
                break;

        }
    } };
    private String getAIUIParams() {
        String params = FucUtil.readAssetFile(this, "cfg/aiui_phone.cfg", "utf-8");

        try {
            JSONObject paramsJson = new JSONObject(params);

            mIsWakeupEnable = !"off".equals(paramsJson.optJSONObject("speech").optString(
                    "wakeup_mode"));
            if (mIsWakeupEnable) {
                FucUtil.copyAssetFolder(this, "ivw", "/sdcard/AIUI/ivw");
            }

            String aiuiVer = paramsJson.optJSONObject("global").optString("aiui_ver", "");
            mIsAIUI_V2 = (TextUtils.isEmpty(aiuiVer) || "2".equals(aiuiVer));

            params = paramsJson.toString();
        } catch (JSONException e) {
            e.printStackTrace();

        }

        return params;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
